package com.example.carrierapp.ui.main;

import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.main.VersionBean;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {
    private Context mContext;

    public MainPresenter(Context context) {
        mContext = context;
    }

    @Override
    public void start() {
    }

    @Override
    public void checkUpdate() {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("appType", "1");
        biddingMap.put("mobileType", "0");
        Flowable<JsonObject> flowable = apiStrores.checkUpdate(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("checkUpdate-onSuccess-" + json);
                VersionBean versionBean = new Gson().fromJson(json, new TypeToken<VersionBean>() {
                }.getType());
                if (getView() != null) {
                    getView().checkUpdateSuccess(versionBean);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("checkUpdate-onFailure-" + msg);
                if (getView() != null) {
                getView().checkUpdateFailde(msg);
                }
            }
        });
    }
}
